package com.gf.controller.mnue;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gf.bean.Menu;
import com.gf.bean.Shop;
import com.gf.service.impl.MenuServiceImpl;
import com.gf.service.impl.ShopServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/*
* 通过接收到的商铺id从而找到该店铺的所有菜品并返回的servlet
* */

@WebServlet(name = "menubyshopid",urlPatterns = "/menubyshopid")
public class MenuServlet extends HttpServlet {
        static MenuServiceImpl menuimpl = null;
        static ShopServiceImpl shopimpl = null;
        static {
                shopimpl=new ShopServiceImpl();
                menuimpl=new MenuServiceImpl();


        }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {






        }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        //创建打印输出对象
        int shop_id =Integer.valueOf( request.getParameter("shop_id"));
        //将传递过来的shopid转化成int对象用来查询对应的菜品
        Shop shop = shopimpl.selectByPrimaryKey(shop_id);
        //查询出id对应的shop对象用于下文输出shop的名字
        List<Menu> menus = menuimpl.selectmenubyshopid(shop_id);
        //通过shopid来查询所有菜品
        JSONObject backjson = new JSONObject();
        //新建json对象
        if (menus!=null){
                //如果menus不等于null就输出店铺的名字和所有对应的店铺数据
                response.setStatus(200);
                backjson.put("state", 200);
                backjson.put("data", menus);
                backjson.put("shopname", shop.getShop_name());


        }else{
                //否则就输出错误信息
                response.setStatus(300);
                backjson.put("state",300);
                backjson.put("data", menus);
        }
        out.println(JSON.toJSON(backjson));
        out.flush();
        out.close();


}
        }
